package com.runen.wnhz.runen.ui.activity.mine;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.common.utils.ACacheUtlis;
import com.runen.wnhz.runen.common.utils.CleanCacheUtil;
import com.runen.wnhz.runen.common.utils.Constants;
import com.runen.wnhz.runen.common.utils.StatusBarCompatUtils;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.UserBean;
import com.runen.wnhz.runen.data.entity.UserUcenterEntity;
import com.runen.wnhz.runen.di.component.ApplicationComponent;
import com.runen.wnhz.runen.di.component.DaggerSettingComponent;
import com.runen.wnhz.runen.di.module.SettingModule;
import com.runen.wnhz.runen.presenter.Contart.SettingCotart;
import com.runen.wnhz.runen.presenter.iPresenter.ISettingPersenter;
import com.runen.wnhz.runen.ui.activity.BaseActivity;
import com.runen.wnhz.runen.widget.TitleBuilder;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018-04-10.
 */

public class AccountSettingActivity extends BaseActivity<ISettingPersenter> implements SettingCotart.View,View.OnClickListener,CompoundButton.OnCheckedChangeListener {



    @BindView(R.id.tv_bind_wx)
    TextView tv_bind_wx;
    @BindView(R.id.tv_bind_qq)
    TextView tv_bind_qq;


    @BindView(R.id.sw_auto_play)   //自动播放下一节
    Switch sw_auto_play;

    @BindView(R.id.sw_wap_watch)   //允许在数据流量环境观看
    Switch sw_wap_watch;

    @BindView(R.id.sw_wap_download)   //允许在数据流量环境下载
    Switch sw_wap_download;

    @BindView(R.id.tv_clear_cache)   //清理缓存
    TextView tv_clear_cache;

   private UserBean reqeustUser;

    /**
     * 是否绑定微信
     */
   private boolean IsBoundToWeChat;
    /**
     * 是否绑定QQ
     */
   private boolean isBoundToQQ;
    /**
     * 当使用流量的时候是否自动播放视频
     */
   private boolean isAutoPlayVideoWhenFlow;
    /**
     * 当使用流量的时候是否可以下载视频
     */
   private boolean isDownloadVideo;


    @Override
    protected int getLayoutView() {
        return R.layout.account_setting_activity_layout;
    }

    @Override
    protected void onTitleClickListen(TitleBuilder.TitleButton clicked) {
        switch (clicked) {
            case LEFT:
                finish();
                break;
            case MIDDLE:
                break;
            case RIGHT:
                break;
            default:
                break;
        }
    }

    private void setListener(){
        sw_wap_watch.setOnCheckedChangeListener(this);
        sw_wap_download.setOnCheckedChangeListener(this);
        sw_auto_play.setOnCheckedChangeListener(this);
    }

    @Override
    protected void initStart() {
        StatusBarCompatUtils.getInstance().TranslucentStatusBar(this,R.color.title_green);
        reqeustUser = ACacheUtlis.getInstance().getReqeustUser(this);
        mPresenter.getUcenter();
        setListener();
        initSwitch();
    }

    private void initSwitch(){
        if(!preferences.getBoolean(Constants.SP_isAutoPlayNext,false)){
            sw_auto_play.setChecked(false);
        }else{
            sw_auto_play.setChecked(true);
        }

        if(!preferences.getBoolean(Constants.SP_isAllowFluxDownload,false)){
            sw_wap_download.setChecked(false);
        }else{
            sw_wap_download.setChecked(true);
        }

        if(!preferences.getBoolean(Constants.SP_isAllowFluxWatch,false)){
            sw_wap_watch.setChecked(false);
        }else{
            sw_wap_watch.setChecked(true);
        }
    }

    @OnClick({R.id.tv_clear_cache})
    public void onClickEvent(View view){
        switch (view.getId()){
            case R.id.tv_clear_cache:
                new AlertDialog.Builder(this)
                        .setTitle("提示")
                        .setMessage("您确定要删除缓存吗?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
//                                CleanCacheUtil.clearAllCache(AccountSettingActivity.this);
//                                getCacheSize();
                                tv_clear_cache.setText("0M");

                            }
                        })
                        .setNegativeButton("取消",null)
                        .create()
                        .show();
                break;
        }
    }
    

    @Override
    public void initToolBar(TitleBuilder mTitleBuilder) {
        mTitleBuilder.setMiddleTitleText("账号设置")
                .setLeftDrawable(R.mipmap.icon_back)
                .seTitleBgRes(R.mipmap.bg_title)
                .setMiddleTitleTextColor(ContextCompat.getColor(this, R.color.text_white));
    }
    
    @Override
    public void setupAcitivtyComponent(ApplicationComponent appComponent) {
        DaggerSettingComponent.builder().applicationComponent(appComponent)
                .settingModule(new SettingModule(this))
                .build().initJect(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getCacheSize();
    }


    /**
     * 获取缓存大小
     */
    private void getCacheSize(){
        String totalcacheSize = "0M";
        try {
            totalcacheSize = CleanCacheUtil.getTotalCacheSize(this);
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.showToast("获取缓存出现异常");
        }
        tv_clear_cache.setText(totalcacheSize);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void showLoadding() {

    }

    @Override
    public void dimissLoding() {

    }

    @Override
    public void showErrorMessage(String msg) {

    }

    @Override
    public String getToken() {
        return reqeustUser.getToken();
    }

    @Override
    public void setLoginError(String info) {

    }

    @Override
    public void getUserMessage(UserUcenterEntity data) {
        tv_bind_wx.setText(data.getIs_weixin());
        tv_bind_qq.setText(data.getIs_qq());
    }

    @Override
    public void showUserMessage(String info) {

    }

    @Override
    public String getType() {
        return null;
    }

    @Override
    public String getOpenid() {
        return null;
    }

    @Override
    public String getImgUrl() {
        return null;
    }

    @Override
    public String getName() {
        return null;
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch (compoundButton.getId()){
            case R.id.sw_auto_play:
                if(b) {
                    editor.putBoolean(Constants.SP_isAutoPlayNext, true).commit();
                }else{
                    editor.putBoolean(Constants.SP_isAutoPlayNext, false).commit();
                }
                break;
            case R.id.sw_wap_download:
                if(b) {
                    editor.putBoolean(Constants.SP_isAllowFluxDownload, true).commit();
                }else{
                    editor.putBoolean(Constants.SP_isAllowFluxDownload, false).commit();
                }
                break;
            case R.id.sw_wap_watch:
                if(b) {
                    editor.putBoolean(Constants.SP_isAllowFluxWatch, true).commit();
                }else{
                    editor.putBoolean(Constants.SP_isAllowFluxWatch, false).commit();
                }
                break;
        }
    }
}
